class Frontend
  def initialize
    @contents = [
      [MenuEntry.new('start game', method(:start_game)), MenuEntry.new('instructions', method(:instructions)), MenuEntry.new('options', method(:options)), MenuEntry.new('quit', method(:quit))],
      [MenuEntry.new('back', method(:main_menu))],
      [MenuEntry.new('level 1', method(:level1_start)), MenuEntry.new('level 2', method(:level2_start)), MenuEntry.new('level 3', method(:level3_start)),MenuEntry.new('back', method(:main_menu))],
      [MenuEntry.new('back', method(:main_menu))]
    ]
    @location = 0
    @selected = 0
    @prevselected = 0
    @draw = true
    @done = true
    @level = 0
    #@font = Gosu::Font.new($screen, Gosu::default_font_name, 20)
    #@font2 = Gosu::Font.new($screen, Gosu::default_font_name, 24)
    @titlesong= Sounds['media/journey.ogg']
    @alpha = 254
    @sound = Sounds["media/sound4.wav"]
    @color1 = Gosu::Color.new(@alpha,150,150, 150)
    @color2 = Gosu::Color.new(@alpha, 255 ,255, 255)
    @color3 = Gosu::Color.new(@alpha,255,255, 255)
    @azurefont = Gosu::Image::load_tiles($screen, "media/characters-solid2.png", 32, 32, false)
    @levelstart = false
    @letters = Hash.new
    @logo = Images['media/pnr.png', false]
    @letters["a"]  = @azurefont[0]
    @letters["b"]  = @azurefont[1]
    @letters["c"]  = @azurefont[2]
    @letters["d"]  = @azurefont[3]
    @letters["e"]  = @azurefont[4]
    @letters["f"]  = @azurefont[5]
    @letters["g"]  = @azurefont[6]
    @letters["h"]  = @azurefont[7]
    @letters["i"]  = @azurefont[8]
    @letters["j"]  = @azurefont[9]
    @letters["k"]  = @azurefont[10]
    @letters["l"]  = @azurefont[11]
    @letters["m"]  = @azurefont[12]
    @letters["n"]  = @azurefont[13]
    @letters["o"]  = @azurefont[14]
    @letters["p"]  = @azurefont[15]
    @letters["q"]  = @azurefont[16]
    @letters["r"]  = @azurefont[17]
    @letters["s"]  = @azurefont[18]
    @letters["t"]  = @azurefont[19]
    @letters["u"]  = @azurefont[20]
    @letters["v"]  = @azurefont[21]
    @letters["w"]  = @azurefont[22]
    @letters["x"]  = @azurefont[23]
    @letters["y"]  = @azurefont[24]
    @letters["z"]  = @azurefont[25]
    @letters["."]  = @azurefont[26]
    @letters[","]  = @azurefont[27]
    @letters["!"]  = @azurefont[28]
    @letters["?"]  = @azurefont[29]
    @letters["0"]  = @azurefont[30]
    @letters["1"]  = @azurefont[31]
    @letters["2"]  = @azurefont[32]
    @letters["3"]  = @azurefont[33]
    @letters["4"]  = @azurefont[34]
    @letters["5"]  = @azurefont[35]
    @letters["6"]  = @azurefont[36]
    @letters["7"]  = @azurefont[37]
    @letters["8"]  = @azurefont[38]
    @letters["9"]  = @azurefont[39]
    @letters[" "]  = @azurefont[40]
    #@sound = Sounds["media/spacebeep.wav"]
    SDL::Mixer.playChannel(0, @titlesong, -1)
    SDL::Mixer.setVolume(-1, 30)
    SDL::Mixer.setVolume(0, 100)
  end
  
  def button_down(id)
    if(id == Gosu::Button::KbDown)
      @selected = (@selected + 1)%(@contents[@location].size)
      SDL::Mixer.playChannel(-1, @sound, 0)
    end
    if(id == Gosu::Button::KbUp)
      @selected = (@selected - 1)%(@contents[@location].size)
      SDL::Mixer.playChannel(-1, @sound, 0)
    end
    
    if(id == Gosu::Button::KbEscape)
      exit
    end
    
    if(id == Gosu::Button::KbReturn or id == Gosu::Button::KbSpace)
      SDL::Mixer.playChannel(-1, @sound, 0)
      @contents[@location][@selected].activate
    end
  end

  def update
     if @draw == false
       $game = Game.new(@level, @letters)
       #destroy
     end
     if @done == false
        @draw = false
        SDL::Mixer.setVolume(0, 0)
     end
  end
  
  def draw
    if @draw
      @logo.draw_rot(600, 430, 0, 0, 0.5,0.5, 0.15,0.15, @color3)

      menu = @contents[@location]
      menu.each_with_index do |item, i|
        if i == @selected
          item.draw_selected(165+i*48, @color1, @letters)
        else
          item.draw(165+i*48, @color2, @letters)
        end
      end

      if @location == 1
        instructions_draw
      end
    
      if @location == 0
      end
      #$screen.draw_line(0, -1, 0xff0000ff, 640, -1, 0xff99CCFF, ZOrder::UI, mode=:additive)
      @color1 = Gosu::Color.new(@alpha,150,150, 150)
      @color2 = Gosu::Color.new(@alpha,255,255,255)
      @color3 = Gosu::Color.new(100,255,255, 255)
    end
  end
  
  def title_draw
  end
  
  def instructions_draw
  end
    
  # menu methods
  
  def options
    @location = 3
    @selected = 0
    @prevselected = 2
  end
  
  def main_menu
    @location = 0
    @selected = @prevselected
  end
  
  def start_game
    @location = 2
    @selected = 0
    @prevselected = 0
  end
  
  def level1_start
    @done = false
    @level = 1
  end
  
  def level2_start
    @done = false
    @level = 2
  end
  
  def level3_start
    @done = false
    @level = 3
  end
  
  def instructions
    @location = 1
    @selected = 0
    @prevselected = 1
  end
  
  def quit
    exit
  end
  
  def nothing
  end
  
  def destroy
    @contents = nil
    @location = nil
    @selected = nil
    @font = nil
    @font2 = nil
    @titlesong= nil
    @arrow = nil
    @shot = nil
    @alpha = nil
    @color1 = nil
    @color2 = nil
    @color3 = nil
    @azurefont = nil
    @levelstart = nil
    @logo = nil
    #@gl_background = nil
    GC.start
  end
  
end

class MenuEntry
  attr_reader :caption, :method
  def initialize(caption, method)
    @caption = caption
    @method = method
    #@font = Gosu::Font.new($screen, Gosu::default_font_name, 36)
  end
  
  def draw(y=0.0, color=0, letters={})
    @length = @caption.length
    @i = 0
    @x = 40
    @length.times{
      @string = @caption[@i, 1]
      @img = letters[@string]
      @img.draw_rot(@x, y, 1, 0, 0.5,0.5, 0.75,0.75, color)
      @i += 1
      @x += 24
    }
  end
  
  def draw_selected(y=0.0, color=0, letters ={})
    @length = @caption.length
    @i = 0
    @x = 40
    @length.times{
      @string = @caption[@i, 1]
      @img = letters[@string]
      @img.draw_rot(@x, y, 1, 0, 0.5,0.5, 0.75,0.75, color)
      @i += 1
      @x += 24
    }
  end
  
  def activate
    @method.call
  end
end
